//
//  FoundBackMineDataViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/1/4.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "FoundBackMineDataViewController.h"
#import "UITextView+WZB.h"
#import "UserServer.h"
#import "ResetPwdViewController.h"
@interface FoundBackMineDataViewController ()
@property (nonatomic, weak) UITextView *inputV;
@property (nonatomic, strong) UserServer *userServer;
@property (nonatomic,weak) UIView *successView;

@end

@implementation FoundBackMineDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    self.title = XLocalized(@"找回信息");
    UILabel*topLab = [UILabel new];
    topLab.textColor = UIColorHex(#909090);
    topLab.font = kDefaultFont(15);
    topLab.text = XLocalized(@"输入私钥   >   找回账户名   >   设置密码");
    topLab.backgroundColor = UIColorHex(#E9E9E9);
    topLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:topLab];
    [topLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(AdaptedWidth(31));
    }];
    
    UITextView *inputV = [UITextView new];
    inputV.layer.cornerRadius = AdaptedWidth(10);
    inputV.backgroundColor = UIColorHex(#F7F7F7);
    inputV.wzb_placeholder = XLocalized(@"请输入您的Bees私钥");
    inputV.wzb_placeholderColor = UIColorHex(#B5B5B5);
    inputV.font = kDefaultFont(16);
    self.inputV = inputV;
    [self.contentView addSubview:inputV];
    [inputV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLab.mas_bottom).offset(AdaptedHeight(40));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(155));
    }];
    
    XThemeFlatButton*sureBtn = [XThemeFlatButton button];
    [sureBtn setTitle:XLocalized(@"验证私钥") forState:UIControlStateNormal];
    [self.contentView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(inputV.mas_bottom).offset(AdaptedHeight(45));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.bottom.mas_equalTo(-AdaptedWidth(100));
    }];
    
    kWeakSelf(self);

    UIView * successView= [UIView new];
    successView.backgroundColor = UIColor.whiteColor;
    self.successView = successView;
    self.successView.hidden = YES;
    [self.contentView addSubview:successView];
    [successView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
    }];
    YYLabel *successLab = [YYLabel new];
    [successView addSubview:successLab];
    [successLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(AdaptedHeight(100));
    }];
    UIButton *copyBtn1 = [UIButton new];
    [successView addSubview:copyBtn1];
    [copyBtn1 setImage:IMAGE_NAMED(@"copy_black") forState:UIControlStateNormal];
    [copyBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(successLab.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(35, 35));
        make.centerY.equalTo(successLab.mas_centerY);
    }];
    XThemeFlatButton *loginBtn = [XThemeFlatButton button];
    [loginBtn setTitle:XLocalized(@"直接登陆") forState:UIControlStateNormal];
    [successView addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(successLab.mas_bottom).offset(AdaptedHeight(130));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    [loginBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
    
    XThemeFlatButton *setBtn = [XThemeFlatButton button];
    [setBtn setTitle:XLocalized(@"重设密码") forState:UIControlStateNormal];
    [successView addSubview:setBtn];
    [setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(AdaptedHeight(33));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.bottom.mas_equalTo(0);
    }];
    [setBtn addTapBlock:^(UIButton * _Nonnull btn) {
        ResetPwdViewController*vc = [ResetPwdViewController new];
        vc.userServer = weakself.userServer;
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
    
    [copyBtn1 addTapBlock:^(UIButton * _Nonnull btn) {
        if (weakself.userServer.account.length) {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            [pab setString:weakself.userServer.account];
            if (pab == nil) {
                [weakself.view makeToast:XLocalized(@"复制失败!")];
            }else
            {
                [weakself.view makeToast:XLocalized(@"复制成功!")];
            }
        }
    }];
    
    kWeakSelf(successLab);
    [sureBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.view endEditing:YES];
        if (!weakself.inputV.text.length) {
            [weakself.view makeToast:weakself.inputV.wzb_placeholder];
            return;
        }
        weakself.userServer.private_key = weakself.inputV.text;
        [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
        [weakself.userServer getAccountComplete:^(BOOL success, NSString * _Nonnull msg, NSString * _Nonnull account) {
            [MBProgressHUD hideHUDForView:weakself.view];
            if (success) {
                weakself.title = XLocalized(@"账户找回");
                weakself.userServer.account = account;
                weakself.successView.hidden = NO;
                NSString *str1 = XLocalized(@"您的Bees账号：");
                NSString *str2 = weakself.userServer.account;
                NSString* bottomText = [NSString stringWithFormat:@"%@%@",str1,str2];
                NSRange range = [bottomText rangeOfString:str2];
                NSMutableAttributedString *text  = [[NSMutableAttributedString alloc] initWithString:bottomText];
                [text setFont:kBoldFont(15) range:[text rangeOfAll]];
                text.color = UIColorHex(#B5B5B5);
                [text setTextHighlightRange:range color:UIColorHex(#FFCC03) backgroundColor:[UIColor clearColor] tapAction:nil];
                weaksuccessLab.attributedText = text;
            }else{
                [weakself.view makeToast:msg];
            }
        }];
    }];
}

- (UserServer*)userServer{
    if (!_userServer) {
        _userServer = [UserServer new];
    }
    return _userServer;
}

@end
